package com.hmdp.controller;

import com.hmdp.entity.User;
import com.hmdp.jwt.JwtUtils;
import com.hmdp.redis.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

import static com.hmdp.utils.DateUtils.getWeek;

@RestController
@RequestMapping("/test")
public class TestSessionController {

    private static final Logger logger = LoggerFactory.getLogger(TestSessionController.class);
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private JwtUtils jwtUtils;

    private String REDIS_TOKEN = "redis:token:";

    private String SESSION_KEY = "session_key";

    private String JWT_KEY = "jwt_key";

    private boolean checkMsg(String msg){
        if (null==msg||"".equals(msg.trim())){
            return false;
        }
        return true;
    }

    @RequestMapping("/session")
    public String setSession(String msg, HttpSession session) {
        if (!checkMsg(msg)){
            return "msg不能为空";
        }
        session.setAttribute(SESSION_KEY,msg);
        return "保存session信息成功"+session.getId();
    }

    @RequestMapping("/getSession")
    public String getSession(HttpSession session) {
        return "session获取成功"+session.getAttribute(SESSION_KEY);
    }

    @RequestMapping("/saveToken")
    public String saveToken(String msg) {
        if (!checkMsg(msg)){
            return "msg不能为空";
        }
        String token = UUID.randomUUID().toString().replace("-","");
        redisUtils.set(token,msg);
        return "保存token信息成功"+token;
    }

    @RequestMapping("/getToken")
    public String getToken(String token) {
        if (!checkMsg(token)){
            return "token不能为空";
        }
        return "获取信息成功:"+redisUtils.get(token,String.class);
    }

    @RequestMapping("/saveTokenWithCookie")
    public String saveTokenWithCookie(String msg, HttpServletResponse response) {
        if (!checkMsg(msg)){
            return "msg不能为空";
        }
        String token = UUID.randomUUID().toString().replace("-","");
        redisUtils.set(token,msg);
        Cookie cookie = new Cookie("token",token);
        cookie.setMaxAge(-1);//会话级别
        response.addCookie(cookie);
        return "保存token信息成功,token:"+token;
    }

    @RequestMapping("/getTokenWithCookie")
    public String getTokenWithCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String token = null;
        for (Cookie cookie:cookies){
            if ("token".equals(cookie.getName())){
                token = cookie.getValue();
                break;
            }
        }
        return "获取token信息成功:"+redisUtils.get(token,String.class);
    }

    @RequestMapping("/saveByJwt")
    public String saveByJwt(String msg) {
        if (!checkMsg(msg)){
            return "msg不能为空";
        }
        //String token = jwtUtils.createToken(JWT_KEY,msg,10);
        User user = new User();
        user.setNickName("luojun");
        user.setPhone("18160764124");
        user.setId(1001L);
        user.setPassword("hhhhh");
        String token = jwtUtils.createToken(JWT_KEY,user,1);
        return "保存token信息成功:"+token;
    }

    @RequestMapping("/getByJwt")
    public String getByJwt(String token) {
        if (!checkMsg(token)){
            return "token不能为空";
        }
        //String msg = (String) jwtUtils.getTokenData(JWT_KEY, token, String.class);
        User user = (User) jwtUtils.getTokenData(JWT_KEY, token, User.class);
        return "获取token信息成功:"+user;
    }

    @RequestMapping("/time")
    public String getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(new Date())+" "+getWeek();

        //return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));

    }

}
